;
(function (call) {
    var main = {
        publishVals: [],
        filePath: '',
        init: function () {
            this.initView();
            this.bindEvt();
        },

        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            $("#publishTime").val(new Date().format('yyyy年MM月dd日hh时'))
            this.getTjrbDataDetail();
        },
        // 产品预览
        productPreview: function (filePath) {
            $("#contentFrame").attr("src", "http://10.226.120.202:80//ioffice/officeView.do?path=" + encodeURI(encodeURI(filePath)));
        },
        // 组装参数
        getParams: function () {
            var fcstTime = new Date().format('yyyyMMdd200000');
            var params = {
                fcstTime: fcstTime,
                element: 'TMAX,TMIN,EDA,WEA',
                startPeriod: 12,
                endPeriod: 24,
                interval: 12,
                count: 0,
                stationType: 0,
                lon: 117.21,
                lat: 39.07
            }
            return params;
        },
        // 初始化12时效
        initTjrbData: function () {
            var that = this;
            loading.add('.cafe-container');
            $.getJSON(api.external.getGdybDataByParamData, that.getParams(), function (res) {
                loading.remove();
                if (!res.success || !res.resultData) return;
                var resultData = res.resultData['117.21_39.07'];
                var wea = weatherUtil.getVal(resultData['WEA'][0][0])
                wea = wea.substr(0, wea.length - 1)
				var tmax = resultData['TMAX'][0][0] == '9999' ? '--' : resultData['TMAX'][0][0];
                $("#dayRain").val(wea)
                $("#dayTmax").val(tmax)
                var wind = resultData['EDA'][2][0].split('_')
                var wp = resultData['EDA'][3][0].split('_')
                if (wind.length === 1) {
                    $("#dayWeather").val(resultData['WEA'][0][0])
                    $("#dayWind").val(wind[0] + ' ' + wp[0])
                } else {
                    $("#dayWeather").val(resultData['WEA'][0][0])
                    $("#dayWind").val(wind[0] + wp[0] + WindUtil.getWindConcat(resultData['EDA'][2][0]) + wind[1] + (wp[1] || wp[0]))
                }
            })
        },
        // 初始化24时效
        initTjrbData2: function () {
            var that = this;
            var params = that.getParams()
            params.startPeriod = params.endPeriod
            params.endPeriod = params.endPeriod + 12
            $.getJSON(api.external.getGdybDataByParamData, params, function (res) {
                if (!res.success || !res.resultData) return;
                var resultData = res.resultData['117.21_39.07'];
                var wea = weatherUtil.getVal(resultData['WEA'][0][0]);
                wea = wea.substr(0, wea.length - 1);
				var tmin = resultData['TMIN'][0][0] == '9999' ? '--' : resultData['TMIN'][0][0];
                $("#nightRain").val(wea);
                $("#nightTmin").val(tmin);
                var wind = resultData['EDA'][2][0].split('_')
                var wp = resultData['EDA'][3][0].split('_')
                if (wind.length === 1) {
                    $("#nightWeather").val(resultData['WEA'][0][0])
                    $("#nightWind").val(wind[0] + ' ' + wp[0])
                } else {
                    $("#nightWeather").val(resultData['WEA'][0][0])
                    $("#nightWind").val(wind[0] + wp[0] + WindUtil.getWindConcat(resultData['EDA'][2][0]) + wind[1] + (wp[1] || wp[0]))
                }
            })
        },
        // 发布天津日报
        publishTjrbData: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var jsonStr = {
                "tempData": {
                    "curTagData": {
                        "i_tjrb_date01": { "val": $("#publishTime").val() },
                        "i_tjrb_wp07": { "val": $("#nightWeather").val() },
                        "i_tjrb_wd04": { "val": $("#dayWind").val() },
                        "i_tjrb_ws05": { "val": "" },
                        "i_tjrb_wd09": { "val": $("#nightWind").val() },
                        "i_tjrb_rain03": { "val": $("#dayRain").val() + '%' },
                        "i_tjrb_mintp11": { "val": $("#nightTmin").val() },
                        "i_tjrb_maxtp06": { "val": $("#dayTmax").val() },
                        "i_tjrb_rain08": { "val": $("#nightRain").val() + '%' },
                        "i_tjrb_wp02": { "val": $("#dayWeather").val() },
                        "i_tjrb_ws10": { "val": "" }
                    },
                }
            }
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                forecasterId: that.userInfo.smuser.iid,
                jsonStr: JSON.stringify(jsonStr),
                taskId: productInfo.taskId
            }
            var url = api.internal.tjrb.publishTjrb;
            loading.add('.cafe-container');
            $.ajax({
                url: url,
                type: "POST",
                dataType: 'json',
                data: params,
                success: function (res) {
                    loading.remove();
                    if (res.isSuccess) {
                        alert("发布成功");
                        CafeJS.menuFunc.setTask();
                        that.getTjrbDataDetail();
                    } else {
                        alert("发布失败");
                    }
                },
            })
        },
        getTjrbDataDetail: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var time = new Date().format('yyyy-MM-dd 20:00:00')
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                queryTime: time
            }
            $.getJSON(api.internal.common.getProductDetail, params, function (res) {
                if (!res.isSuccess) return;
                if (JSON.stringify(res.data) == "{}") {
                    that.initTjrbData();
                    that.initTjrbData2();
                } else {
                    that.getTjrbData(res.data);
                }
            })
        },
        // 获取天津日报
        getTjrbData: function (data) {
            var item = JSON.parse(data.data.content);
            
            item = item.tempData.curTagData;
            var rain03 = item.i_tjrb_rain03.val;
            rain03 = rain03.substr(0, rain03.length - 1);
            var rain08 = item.i_tjrb_rain08.val;
            rain08 = rain08.substr(0, rain08.length - 1);
            $("#publishTime").val(item.i_tjrb_date01.val);
            $("#nightWeather").val(item.i_tjrb_wp07.val);
            $("#nightWind").val(item.i_tjrb_wd09.val);
            $("#dayRain").val(rain03);
            $("#nightTmin").val(item.i_tjrb_mintp11.val);
            $("#dayTmax").val(item.i_tjrb_maxtp06.val);
            $("#nightRain").val(rain08);
            $("#dayWeather").val(item.i_tjrb_wp02.val);
            $("#dayWind").val(item.i_tjrb_wd04.val);
            this.productPreview(data.data.fileName);
            this.filePath = data.data.fileName
        },
        // 传真
        publishFaxData: function () {
            if (!this.filePath) {
                alert("请先发布产品");
                return;
            }
            var faxUserList = "";
            var proType = "天津日报";
            //获取textarea传真有邮件号码
            if ($(".fax_textarea").val() != "" || ($("#faxIsChecked").is(":checked") == true)) {
                //获取fax发送号码
                $("#faxIsChecked").each(function () {
                    if (b = $(this).is(":checked") == true) {
                        var temp = $(this).attr("fax");
                        faxUserList += temp;
                    }
                }
                );
                faxUserList = faxUserList + "," + $(".fax_textarea").val();
                var path = this.filePath.replace('/home/nriet/product_TJ/TJ/', '');
                $.ajax({
                    type: "post",
                    url: api.external.sendFax,
                    async: true,
                    data: {
                        proName: proType,
                        toList: faxUserList,
                        file: path
                    },
                    success: function (res) {
                        alert("传真已发布");
                    }
                });
            } else {
                alert("请输入传真号码");
            }
        },
        // 发送状态查询
        querySendStateData: function () {
            var $body = $("#sendStateTable .table-body").empty();
            var sendunit = "天津日报社";//传真发布单位
            var faxnumber;//传真号码
            var sendTime;//发布时间
            var sendStatus;//发布状态
            var sendType;//发送类型
            var sendDes;//发送描述
            var proName;//产品名称
            //弹窗隐藏和显示
            $.ajax({
                type: "post",
                async: true,
                url: api.external.querySendInfo,
                data: {
                    proName: "天津日报"
                },
                success: function (res) {
                    if (res.success) {
                        var data = res.params.data;
                        for (var i = 0; i < data.length; i++) {
                            sendType = data[i].sendType;
                            if (sendType == "3") {
                                number = data[i].sendType;
                                faxnumber = sendType = data[i].address;
                                sendTime = new Date(sendType = data[i].time);
                                sendDes = data[i].des;
                                proName = data[i].proName;
                                // 判断传真状态
                                if (sendType = data[i].status == "1") {
                                    sendStatus = "发送成功";
                                } else if (sendType = data[i].status == "0") {
                                    sendStatus = "等待接收";
                                } else {
                                    sendStatus = "发送失败";
                                }
                                if (faxnumber == "02223602783") {
                                    sendunit = "天津日报社";
                                } else {
                                    sendunit = "";
                                }
                                var str1 = $("<tr></tr>");
                                var str2 = $("<td>" + (i + 1) + "</td>" + "<td>" + sendunit + "</td>" + "<td>" + faxnumber + "</td>" + "<td>" + sendTime + "</td>" + "<td>" + sendStatus + "</td>" + "<td>" + sendDes + "</td>");
                                str1.append(str2);
                                $body.append(str1);
                            };
                        }
                    } else {
                        alert("查看24小时内传真发送状态失败");
                    }
                }
            });
        },
        faxBoxRemove: function () {
            $(".fax_box").hide();
            $("#faxBtn").removeClass('active');
            this.publishVals = [];
            $(".fax_textarea").val('');
        },
        sendStateBoxRemove: function () {
            $(".send_state_box").hide();
            $("#statusBtn").removeClass('active');
        },
        bindEvt: function () {
            var that = this;
            $(".tjrb_btn div").click(function () {
                var id = $(this).attr('id');
                if (id == 'statusBtn' || id == 'faxBtn') {
                    $(this).toggleClass('active').siblings().removeClass('active');
                } else {
                    that.faxBoxRemove();
                    that.sendStateBoxRemove();
                }
            })
            $("#publishBtn").click(function () {
                that.publishTjrbData();
            })
            $("#returnBtn").click(function () {
                that.initTjrbData();
                that.initTjrbData2();
            })
            $("#faxBtn").click(function () {
                that.sendStateBoxRemove();
                if ($(this).hasClass('active')) {
                    $(".fax_box").show();
                } else {
                    that.faxBoxRemove();
                }
            })
            $(".fax_header i").click(function () {
                that.faxBoxRemove();
            })
            $("#addBtn").click(function () {
                var val = $(".fax_number").val();
                if (!val) {
                    alert('请输入电话号码');
                    return;
                }
                that.publishVals.push(val);
                $(".fax_textarea").val(that.publishVals.join(","));
                $(".fax_number").val('');
            })
            $(".fax_header").draggable({
                container: '.fax_box',
            });
            $(".send_state_header").draggable({
                container: '.send_state_box',
            });
            $(".fax_publish_btn").click(function () {
                that.publishFaxData();
            })
            $(".send_state_header i").click(function () {
                that.sendStateBoxRemove();
            })
            $("#statusBtn").click(function () {
                that.faxBoxRemove();
                if ($(this).hasClass('active')) {
                    $(".send_state_box").show();
                    that.querySendStateData();
                } else {
                    $(".send_state_box").hide();
                }
            })
            $("#downloadBtn").click(function () {
                $("#downloadUrl").attr('href', that.filePath);
                $("#downloadUrl")[0].click();
            })
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
})